package com.cssw.fyzb.tps.util;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;

/**
 * @Author: chenchao
 * @Date: 2020/5/11 12:24
 */
public class IpKit {


    public static String getLocalIP() {
        String ip = "";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
        }
        if (StringUtil.isBlank(ip)) {
            return "127.0.0.1";
        }
        return ip;
    }


    public static String getRealIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }

        return ip;
    }

    public static String getRealIpV2(HttpServletRequest request) {
        String accessIp = request.getHeader("x-forwarded-for");
        return null == accessIp ? request.getRemoteAddr() : accessIp;
    }

}
